local skill = fk.CreateSkill{
  name = "tea__jianju"
}
---@type TrigSkelSpec<TrigFunc>
local jianju_trigger = {
  anim_type = "defensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player.dead or not player:hasSkill(self) then return end
    return target.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if target == player then 
      return room:askToSkillInvoke(player, {
        skill_name = self.name
      })
    elseif player:getMark("@@jianju_loseall-turn") > 0 and not room.current.dead and  room.current ~= player then
      local card = Fk:cloneCard("duel")
      if room.current and not room.current.dead and player:canUseTo(card, room.current, {
        bypass_times = true,
      }) then
        return room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#jianju_ask_for_duel:"..target.id})
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player then
      room:notifySkillInvoked(player, self.name, "defensive")
      player:broadcastSkillInvoke("tea__jianju", table.random{1, 2})
      local num = #table.filter(room:getOtherPlayers(player), function(p)
        return player:inMyAttackRange(p)
      end)
      if num > 0 then
        player:drawCards(num, self.name)
      end
      if not player.dead then
        room:setPlayerMark(player, "tea__jianju_on_effect", 1)
      end
    else
      room:notifySkillInvoked(player, self.name, "defensive")
      player:broadcastSkillInvoke("tea__jianju", table.random{5, 6})
      room:notifySkillInvoked(player, self.name, "offensive")
      local card = Fk:cloneCard("duel")
      card.skillName = self.name
      room:useCard{
        from = player,
        tos = {room.current},
        card = card,
      }
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local jianju_refresh = {
  can_refresh = function(self, event, target, player, data)
    if player.dead or not player:hasSkill("tea__jianju") then return end
    if event == fk.EventPhaseStart and player.phase == Player.Start and target == player then
      return player:getMark("tea__jianju_on_effect") > 0
    elseif event == fk.PreCardUse and data.card and target == player then
      return target == player and table.contains(data.card.skillNames or {}, "tea__jianju") 
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      player.room:setPlayerMark(player, "tea__jianju_on_effect", 0)
    elseif event == fk.PreCardUse then
      player.room:notifySkillInvoked(player, "tea__jianju", "offensive")
      player:broadcastSkillInvoke("tea__jianju", table.random{3, 4})
      if data.card:isVirtual() then
        if table.every(player:getCardIds("h"), function(id)
          return table.contains(data.card.subcards  or {}, id)
        end) then
          player.room:setPlayerMark(player, "@@jianju_loseall-turn", 1)
        end
      end
    end
  end,
}
skill:addEffect("viewas", {
  mute = true,
  pattern = "jink,nullification",
  card_filter = Util.FalseFunc,
  interaction = function()
    local names = {}
    for _, name in ipairs({"jink", "nullification"}) do
      local card = Fk:cloneCard(name)
      if (Fk.currentResponsePattern == nil and Self:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
        table.insertIfNeed(names, card.name)
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  view_as = function(self, player)
    if not self.interaction.data then return end
    if (self.interaction.data == "jink" and not table.find(Self:getCardIds("h"), function(id)
      return Fk:getCardById(id).color == Card.Red
    end)) or (self.interaction.data == "nullification" and not table.find(Self:getCardIds("h"), function(id)
      return Fk:getCardById(id).color == Card.Black
    end)) then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(table.filter(Self:getCardIds("h"), function(id)
      if self.interaction.data == "jink" then
        return Fk:getCardById(id).color == Card.Red
      elseif self.interaction.data == "nullification" then
        return Fk:getCardById(id).color == Card.Black
      end
    end))
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getMark("tea__jianju_on_effect") > 0 and not player:isKongcheng()
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("tea__jianju_on_effect") > 0 and not player:isKongcheng()
  end,
})
skill:addEffect(fk.EventPhaseStart, jianju_trigger)
skill:addEffect(fk.EventPhaseStart, jianju_refresh)
skill:addEffect(fk.PreCardUse, jianju_refresh)
skill:addEffect(fk.PreCardUse, jianju_trigger)
skill:addTest(function (room, me)
  FkTest.runInRoom(function ()
    room:handleAddLoseSkills(me, skill.name)
  end)
  local comp2 = room.players[2]
  local c = room:printCard("nullification", Card.Spade, 13)
  FkTest.setNextReplies(me, {"1", json.encode{
    card = { skill = skill.name, subcards = {}, choice = "nullification" },
    targets = {}
  }})
  FkTest.runInRoom(function ()
    room:obtainCard(me, c)
    GameEvent.Turn:create(TurnData:new(me, "game_rule", { Player.Finish })):exec()
  end)
  local card = room:printCard("archery_attack", Card.Heart, 1)
  FkTest.runInRoom(function ()
    room:useCard{
      from = comp2,
      tos = { me },
      card = card,
    }
  end)
end)
return skill